"use strict";

const baseUrl = 'http://test1.joudou.com:10001';

class Form {
  constructor() {
    $('#debug').text(window.location.href);
    this.isBind();
    this.login();
    this.wechatLogin();
    this.btnBind();
    this.reg();
    this.btnBindOut();
    this.logout();
  }
  getOpenid(name = 'openid') {
      var reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    }
    // initOpenId() {
    //     let openid = this.getParmas('openid') || 'o58sbwF-11UCngZwXoLdsVKgnqD0';
    //     return openid;
    //   }
    // 简单的验证
  isVal() {
      var $pwd = $('#pwd').val();
      var $repwd = $('#repwd').val();
      return $pwd === $repwd && $repwd ? true : false;
    }
  // 初始化判断是否绑定
  isBind(){
    let openId = this.getOpenid();
    let url = `${baseUrl}/binding?openId=${openId}`;
    $.get(url,function(err,data){
        if (!data.username) {
          // 没绑定
          $('#login').removeClass('hide');
          $('#btn-wx').removeClass('hide');
        } else {
          // 已绑定
          $('#userinfo').removeClass('hide');
          $('#username').text(data.username);
          $('#btn-bind-out').removeClass('hide');
        }
    });

  }
    // 绑定用户
  bindUser(parmas) {
      let url = `${baseUrl}/binding`;
      $.post(url, parmas).success(data => {
        $('#login').hide();
        $('.btn-wx').hide();
        // 显示info面板
        $('#userinfo').removeClass('hide');
        $('#btn-bind').removeClass('hide');
      }).error(err => {
        let {message=''} = err.responseJSON;
        $('.err').text(message);
      });
    }
    // 注册
  reg() {
      let $reg = $('#regForm');
      $reg.click(() => {
        if (this.isVal()) {
          let obj = {
            unique_id: $('#username').val(),
            password: $('#pwd').val(),
            password_repeat: $('#repwd').val()
          };
          let url = `${baseUrl}/api/user/signup`;
          $.post(url, obj).success(data => {
            // 注册成功后，自动进行用户绑定
            // self.bindUser({
            //   username: obj.unique_id,
            //   password: obj.password,
            //   openId: this.getOpenid()
            // });
            window.location.href = '/index.html';
          }).error(err => {
            let { message='' } = err.responseJSON;
            $('.err').text(message);
          });
        } else {
          $('.err').text('两次密码不一致，请重新输入！');
        }
      });
    }
    // 登录－－绑定
  login() {
      $('#login').click(() => {
        let loginObj = {
          userName: $('#user').val(),
          password: $('#pwd').val(),
          openId: this.getOpenid()
        };
        this.bindUser(loginObj);
      });
    }
    // 微信登录
  wechatLogin() {
    $('#btn-wx').click(() => {
      this.bindUser({openid:this.getOpenid()});
    });
  }
  // 绑定微信
  btnBind() {
    var $btn = $('#btn-bind');
    $btn.click(() => {
      $('#userinfo').addClass('hide');
      this.bindUser(this.getOpenid());
    });
  }
  // 解除绑定
  btnBindOut() {
    $('#btn-bind-out').click(()=>{
      let openid = this.getOpenid();
      $.ajax({
        url: `${baseUrl}/binding?openId=${openid}`,
        type: 'DELETE',
        success:data=>{
          alert('解绑成功！');
          $('#btn-bind-out').addClass('hide');
          $('#btn-bind').removeClass('hide');
        },
        error:err=>{
          alert(`解绑失败:${err}`);
        }
      })

    });
  }

  logout() {
    $('#btn-logout').click(()=>{
      window.reload();
    });
  }

}

new Form();
